package com.framework.loippi.swagger;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import io.swagger.models.Path;
import io.swagger.models.Swagger;

/**
 * AOP代理类
 * 将接口url中追加模式后缀.json
 * @author wcj
 */
//@Aspect
@Deprecated
public class SwaggerApiSuffixAspect {

	 @AfterReturning(pointcut="execution(public io.swagger.models.Swagger springfox.documentation.swagger2.mappers.ServiceModelToSwagger2MapperImpl.mapDocumentation(..))",
	            returning="swagger")
		public void doBeforeBussinessCheck(Swagger swagger){
	        Map<String, Path> paths = swagger.getPaths();
	        if(null != paths){
	            Map<String, Path> newPaths = new HashMap<String, Path>(paths);
	            paths.clear();
	            Iterator<String> it = newPaths.keySet().iterator();
	            String newKey = "";
	            while(it.hasNext()){
	                String oldKey = it.next();
	                if(oldKey.indexOf(".json") != -1){
	                	 newKey = oldKey;
	                }else{
	                	 newKey = oldKey  + ".json";
	                }
	                paths.put(newKey, newPaths.get(oldKey));
	            }
	            swagger.setPaths(paths);
	            newPaths = null;
	        }
		}
}
